<?php

class image
{
    /**
     * @param $maxwidth
     * @param $file_path
     * @param $name
     *
     * 功能：按规定宽度等比压缩图片（压缩后图片的大小也会被压缩）
     */
//    public function getPic()
//    {
//        $imgList = $_POST["para"];
//        $APPID = "wx34f73cf0f151d567";
//        $APPSECRET = "19c33a64470c8f96d78893f754ae8ad4";
//        $getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$APPSECRET";
//        $returnVal = json_decode($this->getUrlValue($getTokenUrl));
//        $token = $returnVal->access_token;
//        $upFileDir = $_SERVER['DOCUMENT_ROOT'] . "/upload/vote/";
//        $imgArr = "";
//        foreach ($imgList as $key => $val) {
//            $time = time();
//            $url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=$token&media_id=$val";
//            $img = file_get_contents($url);
//            file_put_contents($upFileDir . "$time.jpg", $img);
//            $imgArr .= "/upload/vote/" . $time . ".jpg,";
//            sleep(1);
//        }
//        return substr($imgArr, 0, strlen($imgArr) - 1);
//    }

    public function getPic()
    {
        $imgList = $_POST["para"];
        $APPID = "wx34f73cf0f151d567";
        $APPSECRET = "19c33a64470c8f96d78893f754ae8ad4";
        $getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$APPSECRET";
        $returnVal = json_decode($this->getUrlValue($getTokenUrl));
        $token = $returnVal->access_token;
        $upFileDir = $_SERVER['DOCUMENT_ROOT'] . "/upload/vote/";
        $imgArr = "";
        $time = time();
        $url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=$token&media_id=$imgList";
        $img = file_get_contents($url);
        file_put_contents($upFileDir . "$time.jpg", $img);
        $imgArr .= "/upload/vote/" . $time . ".jpg,";

        return substr($imgArr, 0, strlen($imgArr) - 1);
    }

    /**
     * @param $URL
     * @return mixed
     *
     * 通过URL获取值
     */
    private function getUrlValue($URL)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 2);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        // 为保证第三方服务器与微信服务器之间数据传输的安全性，所有微信接口采用https方式调用，必须使用下面2行代码打开ssl安全校验。
        // 如果在部署过程中代码在此处验证失败，请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 2);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($curl, CURLOPT_URL, $URL);
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }
}

?>